package com.bryantkobe.myspringcode.exception;

import java.util.Map;

/**
 * @description: 异常详情类
 * @author: alienwarezzl
 * @date: 2025/9/22
 */
public record ExceptionDetail(
        String errorCode,
        String errorMessage,
        Map<String, Object> params,
        long timestamp,
        String threadInfo,
        String firstStackTrace) {
    /**
     * 转换为JSON字符串（简化版）
     */
    public String toJson() {
        return String.format(
                "{\"errorCode\":\"%s\",\"message\":\"%s\",\"timestamp\":%d,\"thread\":\"%s\"}",
                errorCode, errorMessage.replace("\"", "\\\""), timestamp, threadInfo
        );
    }

    /**
     * 使用JDK21的字符串模板
     */
    public String toFormattedString() {
        return String.format("\"\"\"\n" +
                "                Error Detail:\n" +
                "                - Code: \\{errorCode}\n" +
                "                - Message: \\{errorMessage}\n" +
                "                - Time: \\{java.time.Instant.ofEpochMilli(timestamp)}\n" +
                "                - Thread: \\{threadInfo}\n" +
                "                - Location: \\{firstStackTrace}\n" +
                "                \"\"\"");
    }

}
